part of '../widgets.dart';

/// [VanImage]属性常量
const kVanImagePlaceholderTextColor = VanCssKey._raw('--van-image-placeholder-text-color');
const kVanImagePlaceholderFontSize = VanCssKey._raw('--van-image-placeholder-font-size');
const kVanImagePlaceholderBackground = VanCssKey._raw('--van-image-placeholder-background');
const kVanImageLoadingIconSize = VanCssKey._raw('--van-image-loading-icon-size');
const kVanImageLoadingIconColor = VanCssKey._raw('--van-image-loading-icon-color');
const kVanImageErrorIconSize = VanCssKey._raw('--van-image-error-icon-size');
const kVanImageErrorIconColor = VanCssKey._raw('--van-image-error-icon-color');

/// [VanImage]主题类
@immutable
class VanImageTheme extends VanProvider<VanImageThemeData> with VanProvide {
  const VanImageTheme({super.key, required super.child, required super.provide});

  static VanImageThemeData of(BuildContext context) => VanProvider.of<VanImageTheme>(context)?.provide ?? VanTheme.of(context).imageTheme;
  static const _prefix = "--van-image-";

  static bool support(VanCssKey key) => key.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanImageThemeData imageTheme = VanImageTheme.of(context);
    if (kVanImagePlaceholderTextColor == key) return imageTheme.placeholderTextColor.get(context);
    if (kVanImagePlaceholderFontSize == key) return imageTheme.placeholderFontSize.get(context);
    if (kVanImagePlaceholderBackground == key) return imageTheme.placeholderBackground.get(context);
    if (kVanImageLoadingIconSize == key) return imageTheme.loadingIconSize.get(context);
    if (kVanImageLoadingIconColor == key) return imageTheme.loadingIconColor.get(context);
    if (kVanImageErrorIconSize == key) return imageTheme.errorIconSize.get(context);
    if (kVanImageErrorIconColor == key) return imageTheme.errorIconColor.get(context);
    return null;
  }
}

/// [VanImage]主题数据类
@immutable
class VanImageThemeData with VanProvide {
  final VanCss<Color> placeholderTextColor;
  final VanCss<double> placeholderFontSize;
  final VanCss<VanBgColor> placeholderBackground;
  final VanCss<double> loadingIconSize;
  final VanCss<Color> loadingIconColor;
  final VanCss<double> errorIconSize;
  final VanCss<Color> errorIconColor;

  /// 构造器
  const VanImageThemeData.raw({
    required this.placeholderTextColor,
    required this.placeholderFontSize,
    required this.placeholderBackground,
    required this.loadingIconSize,
    required this.loadingIconColor,
    required this.errorIconSize,
    required this.errorIconColor,
  });

  /// 拷贝
  VanImageThemeData copyFrom({
    VanCss<Color>? placeholderTextColor,
    VanCss<double>? placeholderFontSize,
    VanCss<VanBgColor>? placeholderBackground,
    VanCss<double>? loadingIconSize,
    VanCss<Color>? loadingIconColor,
    VanCss<double>? errorIconSize,
    VanCss<Color>? errorIconColor,
  }) {
    return VanImageThemeData.raw(
      placeholderTextColor: placeholderTextColor ?? this.placeholderTextColor,
      placeholderFontSize: placeholderFontSize ?? this.placeholderFontSize,
      placeholderBackground: placeholderBackground ?? this.placeholderBackground,
      loadingIconSize: loadingIconSize ?? this.loadingIconSize,
      loadingIconColor: loadingIconColor ?? this.loadingIconColor,
      errorIconSize: errorIconSize ?? this.errorIconSize,
      errorIconColor: errorIconColor ?? this.errorIconColor,
    );
  }

  /// 反序列化
  static VanImageThemeData fromJson(Map<String, dynamic> json) {
    return VanImageThemeData.raw(
      placeholderTextColor: VanCss(json[kVanImagePlaceholderTextColor.key]),
      placeholderFontSize: VanCss(json[kVanImagePlaceholderFontSize.key]),
      placeholderBackground: VanCss(json[kVanImagePlaceholderBackground.key]),
      loadingIconSize: VanCss(json[kVanImageLoadingIconSize.key]),
      loadingIconColor: VanCss(json[kVanImageLoadingIconColor.key]),
      errorIconSize: VanCss(json[kVanImageErrorIconSize.key]),
      errorIconColor: VanCss(json[kVanImageErrorIconColor.key]),
    );
  }
}

/// [VanImage]样式
@immutable
class VanImageStyle with VanProvide {
  static of(BuildContext context) => VanProvider.of<VanImageStyle>(context);

  final double? width;
  final double? height;
  final Color? placeholderTextColor;
  final double? placeholderFontSize;
  final VanBgColor? placeholderBackground;
  final double? loadingIconSize;
  final Color? loadingIconColor;
  final double? errorIconSize;
  final Color? errorIconColor;

  VanImageStyle({
    this.width,
    this.height,
    this.placeholderTextColor,
    this.placeholderFontSize,
    this.placeholderBackground,
    this.loadingIconSize,
    this.loadingIconColor,
    this.errorIconSize,
    this.errorIconColor,
  });

  bool get isNonNull {
    if (width == null) return false;
    if (height == null) return false;
    if (placeholderTextColor == null) return false;
    if (placeholderFontSize == null) return false;
    if (placeholderBackground == null) return false;
    if (loadingIconSize == null) return false;
    if (loadingIconColor == null) return false;
    if (errorIconSize == null) return false;
    if (errorIconColor == null) return false;
    return true;
  }

  VanImageStyle copyFrom({
    double? width,
    double? height,
    Color? placeholderTextColor,
    double? placeholderFontSize,
    VanBgColor? placeholderBackground,
    double? loadingIconSize,
    Color? loadingIconColor,
    double? errorIconSize,
    Color? errorIconColor,
  }) {
    return VanImageStyle(
      width: width,
      height: height,
      placeholderTextColor: placeholderTextColor ?? this.placeholderTextColor,
      placeholderFontSize: placeholderFontSize ?? this.placeholderFontSize,
      placeholderBackground: placeholderBackground ?? this.placeholderBackground,
      loadingIconSize: loadingIconSize ?? this.loadingIconSize,
      loadingIconColor: loadingIconColor ?? this.loadingIconColor,
      errorIconSize: errorIconSize ?? this.errorIconSize,
      errorIconColor: errorIconColor ?? this.errorIconColor,
    );
  }
}
